package com.snopy.leetcode.index1_1000.index101_200;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Question122
 * @description 买卖股票最佳时机
 *
 * @email 77912204@qq.com
 * @date 2022/11/22 8:32
 */
public class Question122 {

    public static void main(String[] args) {
       int[] nums = {1,2,3,4,5};
        int result = maxProfit(nums);
        System.out.println(result);
    }
    public static int maxProfit(int[] prices) {
        int max = 0,sum = 0;
        int pmin = 0;
        for(int i = 1;i < prices.length;i++){
            int m = prices[i]-prices[i-1];
            if(m<0){
                pmin = i;
                sum += max;
                max = 0;
            }
            max = Math.max(prices[i]-prices[pmin],max);
        }
        sum += max;
        return sum;
    }
}
